//
//  ZYCManageCourseSelectDetailViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/7/23.
//

#import "ZYCManageCourseSelectDetailViewController.h"
#import "ZYCCourseDetailHeaderView.h"
#import "ZYCManageCourseSelectDetailCell.h"
#import "ZYCManageCourseTeachTimeCell.h"
#import "ZYCCourseDetailFooterView.h"
#import "ZYCCourseDetailBottomView.h"
#import "ZYCCourseDetailParentBottomView.h"
#import "ZYCCourseRegistrationDetailsViewController.h"
#import "ZYCCourseRegistrationDetailModel.h"
#import "NSDateExtra.h"
@interface ZYCManageCourseSelectDetailViewController ()<UITableViewDelegate,UITableViewDataSource,ZYCCourseDetailBottomViewDelegate,ZYCCourseDetailParentBottomViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ZYCCourseDetailBottomView *bottomView;

@property (nonatomic, strong) ZYCCourseRegistrationDetailModel *detailModel;
@property (nonatomic, assign) CGFloat bottomHeight;
@property (nonatomic, assign) ZYCLoginRoleType roleType;
@end

@implementation ZYCManageCourseSelectDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title  = @"详情";
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomView];
    self.tableView.frame = CGRectMake(0,kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight - CGRectGetHeight(self.bottomView.frame));
    [self loadCourseDetail];
}

//选课报名详情
- (void)loadCourseDetail{
    NSDictionary *params = @{
        @"classPlanId":self.classPlanId,
        @"studentId":self.studentId ? self.studentId : @""
    };
    [MrjSVProgressHUD show];
    [[URLManager sharedURLManager] getUrlString:Appliance_Get_CourseRegistrationDetail requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(id  _Nullable responseObject) {
        [MrjSVProgressHUD dismiss];
        if (SUCCESS(responseObject[@"code"])) {
            self.detailModel = [ZYCCourseRegistrationDetailModel yy_modelWithDictionary:responseObject[@"body"]];
            self.detailModel.courseImg = self.courseImg;
            if ([self.detailModel.maxPeoples integerValue] == self.detailModel.enrollCount) {
                self.bottomView.title = @"报名详情(已满)";
                self.bottomView.status = ZYCBottomStatusTeacherEnrollFull;
            }else{
                self.bottomView.title = [NSString stringWithFormat:@"报名详情(%zd/%zd)",self.detailModel.enrollCount,[self.detailModel.maxPeoples integerValue]];
                self.bottomView.status = ZYCBottomStatusTeacherEnrollNoFull;
            }

            
            [self.tableView reloadData];
        }else{
            [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
        }
    } failure:^(NSError * _Nonnull error) {
        
    }];
}




// 教师端
- (void)courseDetailBottomDidDetail:(ZYCTeacherBottomStatus)status{
    ZYCCourseRegistrationDetailsViewController *vc = [ZYCCourseRegistrationDetailsViewController new];
    vc.model = self.detailModel;
    [self.navigationController pushViewController:vc animated:YES];
  
}


#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 4;
    }else if(section == 1){
        if ([self.detailModel.courseTimeType integerValue] == 3) {//自定义
            return 4 + self.detailModel.startTimeAndEndTime.count;
        }else{
            return 4 + self.detailModel.tcCourseTimeList.count;
        }
    }else{
        return 2;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 || indexPath.section == 2) {
        ZYCManageCourseSelectDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCManageCourseSelectDetailCell class]) forIndexPath:indexPath];
        if (indexPath.section == 0) {
            if (indexPath.row == 0) {
                cell.titleString = @"指定年级:";
                cell.contentString = [NSString stringWithFormat:@"%@年级",self.detailModel.grade];
            }else if(indexPath.row == 1){
                cell.titleString = @"课程类型:";
                cell.contentString = self.detailModel.courseTypeStr;
            }else if(indexPath.row == 2){
                cell.titleString = @"所属课程:";
                cell.contentString = self.detailModel.courseName;
            }else if(indexPath.row == 3){
                cell.titleString = @"授课教师:";
                cell.contentString = self.detailModel.teacherName;
            }
        }else if(indexPath.section == 2){
            if (indexPath.row == 0) {
                cell.titleString = @"计划节次:";
                cell.contentString = self.detailModel.planSection;
            }else if(indexPath.row == 1){
                cell.titleString = @"授课地点:";
                cell.contentString = self.detailModel.courseAddress;
            }
        }
        return cell;
    }else{
        if (indexPath.row == 0 || indexPath.row == 1 || indexPath.row == 2 || indexPath.row == 3) {
            ZYCManageCourseSelectDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCManageCourseSelectDetailCell class]) forIndexPath:indexPath];
            if (indexPath.row == 0) {
                cell.titleString = @"计划人数";
                cell.contentString = [NSString stringWithFormat:@"%@-%@",self.detailModel.minPeoples,self.detailModel.maxPeoples];
            }else if(indexPath.row == 1){
                cell.titleString = @"报名时间:";
                cell.contentString = [NSString stringWithFormat:@"%@-%@",[self returenCorrectTimeString:self.detailModel.enrollStartDate],[self returenCorrectTimeString:self.detailModel.enrollEndDate]];
            }else if(indexPath.row == 2){
                cell.titleString = @"授课有效时间:";
                cell.contentString = [NSString stringWithFormat:@"%@-%@",[self returenCorrectTimeString:self.detailModel.courseStartTime],[self returenCorrectTimeString:self.detailModel.courseEndTime]];
            }else if(indexPath.row == 3){
                cell.titleString = @"授课时间:";
                if ([self.detailModel.courseTimeType integerValue] == 3) {
                    NSArray *teachTimeArr = self.detailModel.tcCourseTimeList;//自定义
                    NSMutableString *stringM = [NSMutableString string];
                    for (ZYCCourseTeachTimeModel *model in teachTimeArr) {
                        [stringM appendString:[NSString stringWithFormat:@"%@、",model.courseDate]];
                    }
                    
                    cell.contentString = [NSString stringWithFormat:@"%@\n%@",self.detailModel.courseTimeTypeStr,[stringM substringToIndex:stringM.length- 1]];
                    
                }else{
                    cell.contentString = self.detailModel.courseTimeTypeStr;
                }
    
            }
            return cell;
        }else{
            ZYCManageCourseTeachTimeCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCManageCourseTeachTimeCell class]) forIndexPath:indexPath];
            NSMutableArray *temp = [NSMutableArray array];
            if ([self.detailModel.courseTimeType integerValue] == 3) {
                NSArray *teachTimeArr = self.detailModel.startTimeAndEndTime;//自定义
                for (ZYCCourseTeachTimeModel *model in teachTimeArr) {
                    [temp addObject:[NSString stringWithFormat:@"%@-%@",model.courseStartTime,model.courseEndTime]];
                }
            }else{
                NSArray *teachTimeArr = self.detailModel.tcCourseTimeList;//授课时间集合
                for (ZYCCourseTeachTimeModel *model in teachTimeArr) {
                    [temp addObject:[NSString stringWithFormat:@"%@:%@-%@",model.weekChine,model.courseStartTime,model.courseEndTime]];
                }
            }
            cell.contentString = temp[indexPath.row - 4];
            return cell;
        }
    }
}



-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 250.0f;
    }
    return 5.0f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return UITableViewAutomaticDimension;
    }
    return 0.01f;
}



- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section == 0) {
        ZYCCourseDetailFooterView *vc = [ZYCCourseDetailFooterView new];
        vc.contentString = self.detailModel.teacherDesc;
        return vc;
    }
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        ZYCCourseDetailHeaderView *vc = [[ZYCCourseDetailHeaderView alloc] init];
        vc.model = self.detailModel;
        return vc;
    }else{
        UIView *vc = [[UIView alloc] init];
        vc.backgroundColor = HEX_RGB(@"#F9F9F9");
        return vc;
    }
}

#pragma mark - getters
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerClass:[ZYCManageCourseSelectDetailCell class] forCellReuseIdentifier:NSStringFromClass([ZYCManageCourseSelectDetailCell class])];
        [_tableView registerClass:[ZYCManageCourseTeachTimeCell class] forCellReuseIdentifier:NSStringFromClass([ZYCManageCourseTeachTimeCell class])];
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedRowHeight = 45.0f;
        _tableView.estimatedSectionFooterHeight  = 145.0f;
        _tableView.sectionFooterHeight = UITableViewAutomaticDimension;
        
    }
    return _tableView;
}

- (ZYCCourseDetailBottomView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[ZYCCourseDetailBottomView alloc] initWithFrame:CGRectMake(0, kScreenHeight - 85, kScreenWidth, 85)];
        _bottomView.delegate = self;
    }
    return _bottomView;
}



#pragma mark -  private method
- (NSString *)returenCorrectTimeString:(NSString *)time{
  return [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:time currenDateFormat:@"yyyy-MM-dd" dateFormat:@"yyyy/MM/dd"]];
    
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
